package com.zxl.live.screen.screen.ui.widget.anim;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.AttributeSet;

import com.zxl.live.screen.R;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018/1/24.
 */

public class BiHuAnimationView extends BaseAnimationView implements SoundPool.OnLoadCompleteListener {

    private Matrix matrix;

    private int rotate = 90;

    private Paint mClearPaint, mPaint;

    private Rect srcRect, dstRect;

    private int soundId;

    private Timer mTimer;

    private SoundPool soundPool;

    private TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            int i = 10 + new Random().nextInt(20) % 11;
            if (i > 12 && i <= 14) {
                soundId = soundPool.load(getContext(), R.raw.bihu2, 1);
            } else if (i > 14 && i <= 16) {
                soundId = soundPool.load(getContext(), R.raw.bihu3, 1);
            } else if (i > 16 && i <= 20) {
                soundId = soundPool.load(getContext(), R.raw.bihu4, 1);
            } else {
                soundId = soundPool.load(getContext(), R.raw.bihu, 1);
            }
        }
    };

    public BiHuAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        matrix = new Matrix();
        mClearPaint = new Paint();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);

        srcRect = new Rect();
        dstRect = new Rect();
        soundPool = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
        soundPool.setOnLoadCompleteListener(this);
    }

    @Override
    public void start() {
        super.start();
        mTimer = new Timer();
        mTimer.schedule(mTimerTask, 3500L, 5000L);
    }

    @Override
    public void stop() {
        super.stop();
        try {
            Thread.sleep(mGapTime);
            mTimer.cancel();
            soundPool.stop(soundId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void drawView() {
        Canvas mCanvas = null;
        try {
            mCanvas = getHolder().lockCanvas();
            Bitmap mBitmap = rotateBitmap(getCurrentBitmap(), rotate);
            if (null != mBitmap) {
                clear(mCanvas);
                srcRect.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
                dstRect.set(0, 0, getWidth(), getHeight());
                mCanvas.drawBitmap(mBitmap, srcRect, dstRect, mPaint);
            }
            if (this.mCurrentIndex == -1 + dataSize()) {
                this.mCurrentIndex = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mCurrentIndex += 1;
            if (mCurrentIndex >= dataSize()) {
                mCurrentIndex = 0;
            }
            if (null != mCanvas) {
                getHolder().unlockCanvasAndPost(mCanvas);
            }
        }
    }

    public void clear(Canvas canvas) {
        mClearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawPaint(mClearPaint);
        mClearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }

    private Bitmap rotateBitmap(Bitmap bitmap, float rotate) {
        matrix.setRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    }

    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        if (status == 0) {
            soundPool.play(sampleId, 1.0F, 1.0F, 1, 0, 1.0F);
        }
    }
}
